package com.mediacloud.app.newsmodule.fragment.recommend;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.eventTrack.TXReportManager;
import com.mediacloud.app.model.component.ComponentItem;
import com.mediacloud.app.model.interfaces.DataInvokeCallBack;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.ArticleListData;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.adaptor.component.XAbsComponentHolder;
import com.mediacloud.app.user.model.UserInfo;
import com.mediacloud.app.model.news.ShowSwitch;
import com.mediacloud.app.newsmodule.activity.RelatedListActivity;
import com.mediacloud.app.newsmodule.fragment.imagetext.RelatedType;
import com.mediacloud.app.newsmodule.utils.ComponentRelativeInvoker;
import com.utils.ClickUtils;

/**
 * Created by yoake on 2017/9/26.
 * 主要处理 标题 更多样式
 */

public abstract class BaseComponentRelatedView extends FrameLayout {
    public ShowSwitch showSwitch;
    protected View mRootView = null;
    public int type = 1;
    public RelatedDataCallBack mDataCallBack;
    public ComponentRelativeInvoker componentRelativeInvoker;


    public String componentId;
    public String article_id;
    public String vid;

    protected XAbsComponentHolder header;

    public BaseComponentRelatedView(Context context) {
        this(context, null);
    }

    public BaseComponentRelatedView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    protected String tipoff_catalogid;

    public BaseComponentRelatedView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initOtherViews();
        this.addView(mRootView);
    }

    /**
     * 更多+标题
     *
     * @param context
     */
    protected void initView(Context context) {
        this.setVisibility(GONE);
        LayoutInflater inflater = LayoutInflater.from(context);
        mRootView = inflater.inflate(setLayoutRes(), null);
        mRootView.setVisibility(View.GONE);
        header = new XAbsComponentHolder(mRootView) {
            @Override
            public boolean isDetail() {
                return true;
            }

            @Override
            public void onClick(View v) {
                ClickUtils.delayClickable(v);
                onMoreClick(v);
                if (componentItem != null && componentItem.title.contains("相关")) {
                    TXReportManager.getInstance().app_jhapp_content_detail_rec_interact("点击更多", articleItem);
                }


                Intent intent = new Intent();
                intent.setClass(getContext(), RelatedListActivity.class);
                if (!TextUtils.isEmpty(tipoff_catalogid)) {
                    intent.putExtra("tipoff_catalogid", tipoff_catalogid);
                }
                intent.putExtra("componentId", componentId);
                intent.putExtra("article_id", article_id);
                intent.putExtra("type", type);
                getContext().startActivity(intent);
            }
        };


    }

    public abstract int setLayoutRes();

    public abstract void initOtherViews();

    public abstract void onDataSuccess(ArticleListData data);

    public abstract void onDataFault(Object data);

    protected ComponentItem componentItem;
    protected ArticleItem articleItem;

    public void setData(ArticleItem item, String article_id, ComponentItem component, String vid, int type) {
        this.componentItem = component;
        articleItem = item;
        componentItem.setText_align(1);
        header.setComponent(componentItem);

        this.vid = vid;
        this.article_id = article_id;
        this.componentId = component.getId() + "";
        componentItem = component;
        this.type = type;
        getRelateNewsList();

        header.getTitleLine().setVisibility(View.GONE);
        int space = getContext().getResources().getDimensionPixelSize(R.dimen.dimen15);
        header.getTitleNameText().setPadding(space, 0, 0, 0);
    }

    public void setData(ArticleItem articleItem, String article_id, ComponentItem component, int type) {
        this.setData(articleItem, article_id, component, null, type);
    }

    /**
     * 获取数据列表
     */
    public void getRelateNewsList() {
        mDataCallBack = new RelatedDataCallBack();
        componentRelativeInvoker = new ComponentRelativeInvoker(mDataCallBack);
        if (UserInfo.isLogin(getContext()) && (componentItem.getType() == RelatedType.HOT_REVIEWS || componentItem.getType() == RelatedType.LASTEST_REVIEW))
            componentRelativeInvoker.getContentComponent(articleItem, componentId, article_id, UserInfo.getUserInfo(getContext()).getUserid(), vid, type, 0, 0);
        else
            componentRelativeInvoker.getContentComponent(articleItem, componentId, article_id, null, vid, type, 0, 0);
    }

    public class RelatedDataCallBack implements DataInvokeCallBack<ArticleListData> {

        @Override
        public void success(ArticleListData data) {
            onDataSuccess(data);
        }

        @Override
        public void fault(Object data) {
            onDataFault(data);
        }
    }


}